/**
 * 
 */
package core;

import java.io.FileNotFoundException;
import java.util.StringTokenizer;

import core.internal.InternalStructure;
import core.internal.builder.command.CommandBuilder;

import util.Logger;


/**
 * @author cipri
 *
 */
public class Sam
{
    private InternalStructure _internal;
    
    public Sam(String path)
    {
        try
        {
            _internal = new InternalStructure(path);
        }
        catch (FileNotFoundException e)
        {
            Logger.logln("The specified directory does not exist. Please provide full path");
        }
    }
    
    public int init()
    {
        String cmd;

        while (true)
        {
            cmd = getCommand();
            StringTokenizer st = new StringTokenizer(cmd);
            CommandBuilder cb = new CommandBuilder();
            
            while (st.hasMoreTokens()) 
            {
                String token = st.nextToken();
                cb.addToken(token);
            }
            
            if (cb.getTokens().size() == 1)
            {
                if (cmd.compareTo("display-names") == 0) 
                {
                    _internal.print();
                    Logger.logln("-------------------------");
                }
                if (cmd.compareTo("exit") == 0) 
                {
                    break;
                }    
            }
            else
            {
                if (cb.getTokens().size() > 4)
                {
                    Logger.logln("Too many arguments");
                }
                else
                {
                    if (cb.getTokens().get(0).compareTo("write-archive") == 0)
                    {
                        if (cb.getTokens().size() == 4)
                        {
                            if (cb.getTokens().get(2).compareTo("-f") == 0)
                            {
                                writeArchive(cb.getTokens().get(1), cb.getTokens().get(3));    
                            }
                            else
                            {
                                Logger.logln("Use the -f filter specifier");
                            }
                        }
                        else
                        {
                            writeArchive(cb.getTokens().get(1), "");
                        }
                    }
                    if (cb.getTokens().get(0).compareTo("write-files") == 0)
                    {
                        if (cb.getTokens().size() == 4)
                        {
                            if (cb.getTokens().get(2).compareTo("-f") == 0)
                            {
                                writeFiles(cb.getTokens().get(1), cb.getTokens().get(3));    
                            }
                            else
                            {
                                Logger.logln("Use the -f filter specifier");
                            }
                        }
                        else
                        {
                            writeFiles(cb.getTokens().get(1), "");
                        }
                    }
                }
            }
        }
        return 0;
    }

    private void writeFiles(String path, String filter)
    {
        _internal.writeFiles(path, filter);
    }

    private void writeArchive(String path, String filter)
    {
        _internal.writeArchive(path, filter);
    }

    public void print()
    {
        _internal.print();
    }
    
    private String getCommand()
    {
        Logger.log("sam:\\>");
        return Logger.getCommand();
    }
}
